// firstPackage/pages/customManager/addCustomer/addCustomer.js
import { createClientUser, updateClientUser } from "../../../../service/custom_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    saveTitle: "保存",
    formData: {
      "customerName": "",
      "customerPhone": "",
      "gender": null,
      "hiddenPhone": "",
      "idCard": "",
      zone: "",//区域
      floor: "",//意向楼层
      houseAge: "",//房龄
      firstPreMoney: "",//首付预算
      totalPreMoney: "",//总价预算
      unitPreMoney: "",//单价预算
      payType: "",//付款方式
      buyDegree: "",//购房意向程度
      buyHouseCategory: "",//购买类型

    },
    isShowAll: false,//是否展开所有数据
    hidePhoneThree: "",//前三位
    hidePhoneFour: "",//后四位
    showSexPop: false,
    sexArr: [
      {
        value: 1,
        label: "男"
      },
      {
        value: 2,
        label: "女"
      },
    ],
    reportPhoneReq: false,
    reportCardReq: false,
    isReportFrom: false,
    showCityArea: false,
    showSinglePop: false,
    showPopType: "",
    showPopTyepTitle: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isFrom) {
      this.setData({
        reportPhoneReq: options.allPhone,
        reportCardReq: options.idCard,
        isReportFrom: options.isFrom
      })
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },


  //保存客户
  saveClientAction() {
    console.log("JJLog:要提交的信息:" + JSON.stringify(this.data.formData))
    // return;
    if (!this.data.formData.customerName) {
      wx.showToast({
        title: '请输入客户姓名',
        icon: "none"
      })
      return;
    }
    if (this.data.formData.hiddenPhone.length != 11) {
      wx.showToast({
        title: '请输入客户非全号手机号',
        icon: "none"
      })
      return;
    }
    if (!this.data.formData.gender) {
      wx.showToast({
        title: '请选择客户性别',
        icon: "none"
      })
      return;
    }
    //要求全号报备
    if (!this.data.formData.customerPhone && this.data.reportPhoneReq == "true") {
      wx.showToast({
        title: '请输入客户手机号全号',
        icon: "none"
      })
      return;
    }
    //要求身份证后四位报备
    if (!this.data.formData.idCard && this.data.reportCardReq == "true") {
      wx.showToast({
        title: '请输入客户身份证后四位',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:保存的客户是:" + JSON.stringify(this.data.formData));
    if (this.data.saveTitle == "更新") {
      this.updateClientUser(this.data.formData);
    } else {
      this.createClientUser(this.data.formData);
    }

  },
  createClientUser(data) {
    wx.showLoading({
      title: '创建中...',
    })
    createClientUser(data).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '创建成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: '创建失败',
        icon: "none"
      })
    })
  },

  //公共的单项选择
  commonSingleSelect(e) {
    let type = e.currentTarget.dataset.type;
    console.log("JJLog:type:" + type);
    if (type == "zone") {
      this.setData({
        showCityArea: true
      })
    }
    if (type == "yx") {
      this.setData({
        showPopType: "yx",
        showPopTyepTitle: "意向程度",
        showSexPop: true,
      })
    }
    if (type == "lx") {
      this.setData({
        showPopType: "lx",
        showPopTyepTitle: "购买类型",
        showSexPop: true,
      })
    }
  },



  updateClientUser(data) {
    wx.showLoading({
      title: '更新中...',
    })
    updateClientUser(data).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '更新成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      console.log("JJLog:更新失败:" + JSON.stringify(err));
      wx.hideLoading();
      wx.showToast({
        title: '更新失败',
        icon: "none"
      })
    })
  },
  //输入绑定
  bindInputValue(e) {
    let type = e.currentTarget.dataset.type;
    let key = `formData.${type}`
    this.setData({
      [key]: e.detail.value
    })
  },
  //隐藏号码显示
  bindhidePhone(e) {
    let value = e.detail.value;
    let type = e.currentTarget.dataset.type;
    if (type == "three") {
      this.setData({
        hidePhoneThree: value
      })
    } else {
      this.setData({
        hidePhoneFour: value
      })
    }
    this.setData({
      "formData.hiddenPhone": this.data.hidePhoneThree + "****" + this.data.hidePhoneFour
    })
  },
  //号码隐藏处理:
  phoneHideDeal(phone) {
    if (phone.length <= 3) {
      return phone + "****"
    } else if (phone.length <= 7) {
      // if (phone.length == 4) {
      //   return phone.substring(0, 3) + "*"
      // } else if (phone.length == 5) {
      //   return phone.substring(0, 3) + "**"
      // } else if (phone.length == 6) {
      //   return phone.substring(0, 3) + "***"
      // } else if (phone.length == 7) {
      //   return phone.substring(0, 3) + "****"
      // }
      return phone.substring(0, 3) + "****" + phone.substring(4);
    }
    // else if (phone.length > 7) {
    //   return phone.substring(0, 3) + "****" + phone.substring(7)
    // }
  },
  //选择性别弹窗
  showSelectSexPop() {
    this.setData({
      showSexPop: true,
      showPopType: "sex",
      showPopTyepTitle: "选择性别"
    })
  },
  sureSexSelect(e) {
    console.log("JJLog:选择结果:" + JSON.stringify(e))
    let type = e.detail.type;
    if (type == "sex") {
      this.setData({
        'formData.gender': e.detail.result.value,
        showSexPop: false
      })
    } else if (type == "yx") {
      this.setData({
        'formData.buyDegree': e.detail.result.value,
        showSexPop: false
      })
    } else if (type == "lx") {
      this.setData({
        'formData.buyHouseCategory': e.detail.result.value,
        showSexPop: false
      })
    }

  },
  //是否展开更多客户信息
  foldSecondTagAction() {
    this.setData({
      isShowAll: this.data.isShowAll ? false : true
    })
  },
  cityPopHidden() {
    this.setData({
      showCityArea: false
    })
  },

  //确定选择区域
  cityPopConfirm(e) {
    let result = e.detail.result;
    // [{ "code": "510000", "name": "四川省" }, { "code": "510400", "name": "攀枝花市" }]
    this.setData({
      'formData.zone': result[2].name,
      showCityArea: false
    })
  },

  hidePopup() {
    this.setData({
      showSexPop: false
    })
  },
})